/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package EventHandler;

import app.bus.ProductModel;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mvc.Event;

/**
 *
 * @author Tuan Long
 */
public class SearchProduct implements Event{

    private static String txt="";
    @Override
    public String EventHandler(HttpServletRequest request, HttpServletResponse response) {
        int page = 1;
        int records = 4;
        if (request.getParameter("page") != null) {
            page = Integer.parseInt(request.getParameter("page"));
        }
        int start_row = (page * records) - (records - 1);
        int end_row = start_row + (records - 1);
        if(request.getParameter("txtSearch")!=null){
            txt = request.getParameter("txtSearch");
        }        
        List list = ProductModel.searchProductByName(txt,start_row,end_row);
        int total_rows = 30;
        int no_page = ((int)Math.ceil(total_rows / records));       
        request.setAttribute("list", list);
        request.setAttribute("start_row", start_row);
        request.setAttribute("end_row", end_row);
        request.setAttribute("no_page", no_page+1);
        request.setAttribute("currentPage", page);
        return "SearchProduct.jsp";
    }
    
}
